package com.novel.service.impl.review;

import com.novel.entiy.Novel;
import com.novel.entiy.TpUser;
import com.novel.entiy.dto.ReviewDTO;
import com.novel.mapper.NovelMapper;
import com.novel.mapper.TpUserMapper;
import com.novel.service.ReviewService;
import com.novel.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * @author tanghao
 * @date 2022/4/16
 */
@Service
public class SignReviewServiceImpl implements ReviewService {

    @Resource
    private NovelMapper novelMapper;

    @Value("${spring.mail.nickname}")
    private String nickname;

    @Resource
    private JavaMailSenderImpl mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Resource
    private TpUserMapper tpUserMapper;

    @Resource
    private UserService userService;
    /**
     * 同意审批
     *
     * @param reviewDTO 审批参数
     */
    @Override
    public void agreeReview(ReviewDTO reviewDTO) {
        TpUser tpUser = tpUserMapper.selectById(reviewDTO.getMainId());
        tpUser.setIsSign(1);
        tpUser.setSignTime(new Date());
        tpUserMapper.updateById(tpUser);
        //邮箱通知用户成为小说通过
        try {
            MimeMessage mimeMailMessage = mailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(nickname + '<' + from + '>');
            mimeMessageHelper.setTo(tpUser.getEmail());
            mimeMessageHelper.setSentDate(new Date());
            mimeMessageHelper.setText("<h3>恭喜你和本网站正式签约成功！！！ 期待和你今后的合作</h3>" +
                    "<img src='https://novel-chihuo.oss-cn-beijing.aliyuncs.com/a.jpg", true);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 拒绝审批
     *
     * @param reviewDTO 审批参数
     */
    @Override
    public void rejectReview(ReviewDTO reviewDTO) {
        TpUser tpUser = tpUserMapper.selectById(reviewDTO.getMainId());
        tpUser.setIsSign(0);
        tpUserMapper.updateById(tpUser);
        //邮箱通知用户作品审核失败
        try {
            MimeMessage mimeMailMessage = mailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
            mimeMessageHelper.setFrom(nickname + '<' + from + '>');
            mimeMessageHelper.setTo(tpUser.getEmail());
            mimeMessageHelper.setSentDate(new Date());
            mimeMessageHelper.setText("<h3>你与本网站的签约申请,审核不通过,请继续完善个人资料</h3>" +
                    "<img src='https://novel-chihuo.oss-cn-beijing.aliyuncs.com/u%3D1326779493%2C3132618811%26fm%3D253%26fmt%3Dauto%26app%3D138%26f%3DJPEG.jpg", true);
            mailSender.send(mimeMailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
